package api

import (
	"singo/service"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

// ProjectCreate 新增项目
// @Summary 新增项目
// @Router /api/v1/project/create [post]
// @tags 项目相关
// @accept application/json
// @Param name query string true "项目名称"
// @Param organization_id query string true "小组ID"
// @Param intro query string false "介绍"
// @Param avatar query string false "头像"
// @Param status query int false "状态"
// @Param created_by query string true "创建人"
// @Param deadline_at query string false "截止日期"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func ProjectCreate(c *gin.Context) {
	var service service.ProjectCreateService
	session := sessions.Default(c)
	userUID := session.Get("user_uid")

	if err := c.ShouldBind(&service); err == nil {
		res := service.ProjectCreate(userUID.(string))
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// ProjectList 项目列表接口
// @Summary 项目列表
// @Router /api/v1/projects [get]
// @tags 项目相关
// @accept application/json
// @Param organization_id query string false "组织ID"
// @Param pageNo query int false "页码"
// @Param pageSize query int false "条数"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func ProjectList(c *gin.Context) {
	service := service.ListProjectService{}
	service.OrganizationUID = c.DefaultPostForm("organization_uid", "")

	if err := c.ShouldBind(&service); err == nil {
		res := service.ProjectList()
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// ProjectShow 项目详情接口
// @Summary 项目详情
// @Router /api/v1/project/{id} [get]
// @tags 项目相关
// @accept application/json
// @Param id path string false "项目ID"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func ProjectShow(c *gin.Context) {
	service := service.ProjectShowService{}
	res := service.ProjectShow(c.Param("id"))
	c.JSON(200, res)
}

// ProjectUpdate 更新项目接口
// @Summary 更新项目
// @Router /api/v1/project/{id} [put]
// @tags 项目相关
// @accept application/json
// @Param id path string false "项目ID"
// @Param name query string false "项目名称"
// @Param intro query string false "项目介绍"
// @Param avatar query string false "项目头像"
// @Param status query int false "状态"
// @Param created_by query string false "创建人"
// @Param deadline_at query string false "截止时间"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func ProjectUpdate(c *gin.Context) {
	service := service.ProjectUpdateService{}

	if err := c.ShouldBind(&service); err == nil {
		res := service.ProjectUpdate()
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// ProjectDelete 删除项目接口
// @Summary 删除项目
// @Router /api/v1/project/{id} [delete]
// @tags 项目相关
// @accept application/json
// @Param id path string false "项目ID"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func ProjectDelete(c *gin.Context) {
	service := service.ProjectDeleteService{}
	res := service.ProjectDelete(c.Param("id"))
	c.JSON(200, res)
}
